<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController
{
    //
    public function index()
    {
        return view('admin.index');
    }

    public function info()
    {
        return view('admin.info');
    }

    public function quit()
    {
        session(['user'=>null]);
        return redirect('admin.login');
    }

    /**
     * 修改管理员密码
     * create by wenQing
     */
    public function pass()
    {
        if($input = Input::all())
        {
            $rule = [
                'password' => 'required|between:6,20|confirmed'
            ];
            $msg = [
                'password.required' => '密码必须要填写',
                'password.between' => '密码必须在6到20位之间',
                'password.confirmed' => '密码确认不匹配',
            ];
            $validate = Validator::make($input,$rule,$msg);
            if($validate->passes())
            {
                $user = User::first();
                $pass = Crypt::decrypt($user->user_pass);
                if($input['password_o'] == $pass)
                {
                    $user->user_pass = Crypt::encrypt($input['password']);
                    $user->update();
                    return redirect('admin/index');
                }
                else
                {
                    return back()->with('errors','原密码错误');
                }
            }
            else
            {
                //dd($validate->errors()->all());
                return back()->withErrors($validate);
            }
        }

        return view('admin.pass');
    }
}
